package com.bst.bsj.bops.web.biz.impl;

import java.util.List;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.web.biz.UserService;
import com.bst.bsj.bops.web.biz.mongo.AbstractMongoDao;
import com.bst.bsj.common.dal.dataobject.User;

@Service("userService")
public class UserServiceImpl extends AbstractMongoDao<User> implements UserService {

  @Override
  public void saveUser(User user) {
    this.insert(user);
  }

  @Override
  public User findOneUserByName(String name) {
    // TODO Auto-generated method stub
    Query query = new Query();
    query.addCriteria(Criteria.where("name").is(name));
    return this.queryOne(query);
  }

  @Override
  public List<User> findUsersByName(String name) {
    // TODO Auto-generated method stub
    Query query = new Query();
    query.addCriteria(Criteria.where("name").is(name));
    return this.queryList(query);
  }

  @Override
  protected Class<User> getEntityClass() {
    // TODO Auto-generated method stub
    return User.class;
  }

  @Override
  public void DeleteById(String Id) {
    // TODO Auto-generated method stub
    this.deleteById(Id);
  }

  @Override
  public void UpdateUser(User user) {
    // TODO Auto-generated method stub
    Criteria criteria = Criteria.where("id").gt(user.getId());
    Query query = new Query(criteria);
    Update update = Update.update("name", user.getName()).set("age", user.getAge());
    this.updateMulti(query, update);
  }

  @Override
  public Long findUsersPageCount(String name) {
    // TODO Auto-generated method stub
    Query query = new Query();
    query.addCriteria(Criteria.where("name").is(name));
    // query.addCriteria(Criteria.where("age").is(age));
    return this.getPageCount(query);
  }

  @Override
  public List<User> findUsersPages(String name, int start, int size) {
    // TODO Auto-generated method stub
    Query query = new Query();
    query.addCriteria(Criteria.where("name").is(name));
    return this.getPage(query, start, size);
  }

}
